package org.example.service.market;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.dao.dao.market.CCommodityDao;
import org.example.dao.dao.market.CCommodityKindDao;
import org.example.dao.dao.market.CImageDao;
import org.example.dao.dto.market.CommodityDto;
import org.example.dao.entity.market.CCommodity;
import org.example.dao.entity.market.CCommodityKind;
import org.example.dao.entity.market.CImage;
import org.example.dao.entity.market.CKind;
import org.example.dao.vo.CommodityKindVo;
import org.example.skytool.commom.IdWorker;
import org.example.skytool.oss.minio.MinioConstant;
import org.example.skytool.oss.minio.MinioUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 22449
 */
@Service
public class CommodityService {

    @Resource
    private CCommodityDao cCommodityDao;

    @Resource
    private CImageDao cImageDao;

    @Resource
    private IdWorker idWorker;

    @Resource
    private MinioUtil minioUtil;

    @Resource
    private CCommodityKindDao cCommodityKindDao;

    /**
     * 添加商品(商户)
     */
    public CCommodity addCommodity(List<Long> kindIds, CCommodity commodity) {
        commodity.setId(idWorker.nextId());
        for (Long kindId : kindIds) {
            CCommodityKind cCommodityKind = new CCommodityKind();
            cCommodityKind.setId(idWorker.nextId());
            cCommodityKind.setCommodityId(commodity.getId());
            cCommodityKind.setKindId(kindId);
            cCommodityKindDao.insert(cCommodityKind);
        }
        cCommodityDao.insert(commodity);
        return commodity;
    }

    /**
     * 查询商店有哪些商品符合商品类型(商户)
     */
    public IPage<CCommodity> selectCommodityByKind(Long storeId, Long kindId, Long limit, Long start) {
        List<CCommodity> commodityByKind = cCommodityDao.getCommodityByKind(storeId, kindId,
                (start - 1) * limit, limit);
        Page<CCommodity> page = new Page<>(start, limit);
        Long total = cCommodityDao.getCommoditiesNum(storeId, kindId);
        page.setTotal(total);
        page.setRecords(commodityByKind);
        return page;
    }

    /**
     * 查询有哪些商品符合商品类型(管理员)
     */
    public IPage<CCommodity> selectAllCommodityByKind(Long kindId, Long limit, Long start) {
        List<CCommodity> commodityByKind = cCommodityDao.getCommodityByKind(null, kindId,
                (start - 1) * limit, limit);
        Page<CCommodity> page = new Page<>(start, limit);
        Long total = cCommodityDao.getCommoditiesNum(null, kindId);
        page.setTotal(total);
        page.setRecords(commodityByKind);
        return page;
    }

    /**
     * 查询该商店有哪些商品(商户)
     */
    public IPage<CCommodity> selectCommodityByStore(Long limit, Long start, Long storeId) {
        LambdaQueryWrapper<CCommodity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CCommodity::getStoreId, storeId);
        Page<CCommodity> cCommodityPage = cCommodityDao.selectPage(new Page<>(start, limit), lambdaQueryWrapper);
        return cCommodityPage;
    }

    /**
     * 查询所有商品(管理)
     */
    public IPage<CCommodity> selectCommodity(Long limit, Long start) {
        LambdaQueryWrapper<CCommodity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(CCommodity::getSales);
        Page<CCommodity> cCommodityPage = cCommodityDao.selectPage(new Page<>(start, limit), lambdaQueryWrapper);
        return cCommodityPage;
    }

    /**
     * 条件查询商品(商户)
     */
    public IPage<CCommodity> selectCommodityByCondition() {
        return null;
    }

    /**
     * 修改商品信息(商户)
     */
    public CCommodity updateCommodity(CCommodity commodity) {
        commodity.setUpdateTime(new Date());
        cCommodityDao.updateById(commodity);
        return commodity;
    }

    /**
     * 修改商品的类型(商户)
     */
    public CommodityKindVo updateCommodityKind(CommodityKindVo commodityKindVo) {
        CCommodity commodity = commodityKindVo.getCommodity();
        Long commodityId = commodity.getId();
        LambdaQueryWrapper<CCommodityKind> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CCommodityKind::getCommodityId, commodityId);
        cCommodityKindDao.delete(lambdaQueryWrapper);
        List<Long> kindIds = commodityKindVo.getKindIds();
        for (Long kindId : kindIds) {
            CCommodityKind cCommodityKind = new CCommodityKind();
            cCommodityKind.setKindId(kindId);
            cCommodityKind.setCommodityId(commodityId);
            cCommodityKind.setId(idWorker.nextId());
            cCommodityKindDao.insert(cCommodityKind);
        }
        commodity.setUpdateTime(new Date());
        cCommodityDao.updateById(commodity);
        return commodityKindVo;
    }

    /**
     * 修改商品主页图标(商户)
     */
    public CCommodity updateCommodityIcon(Long commodityId, MultipartFile file) {
        CCommodity commodity = cCommodityDao.selectById(commodityId);
        String url = minioUtil.upload(file, MinioConstant.COMMODITY, commodityId.toString());
        commodity.setIcon(url);
        commodity.setUpdateTime(new Date());
        cCommodityDao.updateById(commodity);
        return commodity;
    }

    /**
     * 上传店铺图像(商户)
     */
    public List<CImage> uploadCommodityImages(MultipartFile[] files, Long commodityId) {
        List<CImage> cImageList = new ArrayList<>();
        int temp = 1;
        for (MultipartFile file : files) {
            String url = minioUtil.upload(file, MinioConstant.COMMODITY, commodityId.toString() + temp);
            CImage cImage = new CImage();
            cImage.setId(idWorker.nextId());
            cImage.setUrl(url);
            cImage.setLinkId(commodityId);
            cImageList.add(cImage);
            cImageDao.insert(cImage);
            temp++;
        }
        return cImageList;
    }

    /**
     * 查看店铺图像(商户)
     */
    public List<CImage> selectCommodityImages(Long commodityId) {
        List<CImage> cImageList = cImageDao.selectImages(null, commodityId);
        return cImageList;
    }

    /**
     * 删除商品(商户)
     */
    public void deleteCommodity(CCommodity commodity) {
        commodity.setUpdateTime(new Date());
        commodity.setIsValid(0);
        cCommodityDao.updateById(commodity);
    }

    /**
     * 导出导入模板(商户)
     */
    public void downloadCommodity() {

    }

    /**
     * 按模板导入商品(商户)
     */
    public void importCommodity() {

    }

    public List<CCommodity> getCommodityByIds(List ids){
        LambdaQueryWrapper<CCommodity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CCommodity::getIsValid,1);
        lambdaQueryWrapper.in(CCommodity::getId,ids);
        return cCommodityDao.selectList(lambdaQueryWrapper);
    }

    public List<CommodityDto> getCommodityDtoByIds(List ids){
        return cCommodityDao.getCommodityDtoByIds(ids);
    }

}
